import pygame
from pygame.locals import *
from Constant import *

"""
Sprite
------
A class for storing and handeling non-animated 2D images.
"""

class sprite:
    image = None
    x = 0
    y = 0
    w = None
    h = None
    _rectangle = None

    #Class "Constructor"
    def __init__(self, fName, w=0,h=0):
        self.image = pygame.image.load(pathImages + fName)
        self.image.set_colorkey(self.image.get_at((0,0)))
        self._rectangle = self.image.get_rect()
        self.x = self._rectangle.left
        self.y = self._rectangle.top
        self.w = self._rectangle.right - self._rectangle.left
        self.h = self._rectangle.bottom - self._rectangle.top
        
        if w != 0:
            self.image = pygame.transform.scale(self.image, (w,h))
            self.w = w
            self.h = h
        
    def Update(self):
        pass
    
    #Move the sprite by a speed
    def move(self, vx, vy):
        self.x = self.x + vx
        self.y = self.y + vy
        self._posToRect()
    
    #Set the position of the sprite
    def setPOS(self, x, y):
        self.x = x
        self.y = y
        self._posToRect()
        
    #Don't Call. This sets the rectangle after a change
    def _posToRect(self):
        self._rectangle.left = self.x
        self._rectangle.top = self.y
        self._rectangle.right = self.w + self.x
        self._rectangle.bottom = self.h + self.y
    
    # Draws the sprite to the screen <.<
    def Draw(self, screen):
        screen.blit(self.image, self._rectangle)

"""
aSprite
------
A class for storing and handeling animated 2D images.
"""

class aSprite(sprite):
    sheet = []
    
    sheetW = 0
    sheetH = 0
    
    frame = 0
    iter = 0
    fps = 0
    ticks = 0
    
    _animations = []
    currentA = 0
    
    repeating = False
    
    def __init__(self, fName, w, h, fps=100):
        tmp = pygame.image.load(pathImages + fName)
        
        tmpRect = tmp.get_rect()
        self.sheetW = tmpRect.right
        self.sheetH = tmpRect.bottom
        
        tmpRect = pygame.Rect(0, 0, w, h)
        
        self._rectangle = tmpRect
        
        self.x = 0
        self.y = 0
        self.w = w
        self.h = h
        
        self.fps = fps
        
        self._split(tmp)
    
    #Add an animation sequence to the list
    def addAnimation(self, strip):
        self._animations.append(strip)
    
    #Start a 1 Time animation
    def start(self, i):
        self.currentA = i
        self.frame = self._animations[i][0]
        self.repeating = False
    
    #Enter which animation to repeate
    def repeat(self, i):
        self.currentA = i
        self.frame = self._animations[i][0]
        self.iter = 0
        self.repeating = True
    
    #Go to the next frame in the animation sequence
    def Update(self):
        if self.iter + 1 == len(self._animations[self.currentA]):
            if self.repeating:
                self.frame = self._animations[self.currentA][0]
                self.iter = 0
            else:
                self.end()
        else:
            self.iter += 1
            self.frame = self._animations[self.currentA][self.iter]
    
    #Stop the animation. Go back to the first frame.
    def end(self):
        self.frame = 0
        self.currentA = 0
        self.iter = 0
        self.repeating = False
    
    #Don't Call. This splites the image into the desired frames
    def _split(self, master):
        framesW = self.sheetW/self.w
        framesT = self.sheetH/self.h
        
        for j in range(framesT):
            for i in range(framesW):
                tmp = pygame.Surface((self.w, self.h))
                tmp.blit(master, (0,0), ((i*self.w, j*self.h),(self.w, self.h)))
                tmp.set_colorkey(tmp.get_at((0,0)))
                self.sheet.append(tmp)
    
    #Draw the animated sprit
    def Draw(self, screen):
        screen.blit(self.sheet[self.frame], self._rectangle)

"""
Manager
------
A class for storing all sprites being used.
It's responsible for:

Drawing sprites that are in view
Deleting dead sprites
Updated animation cycle on animated sprites
--Will be added with animated sprite class :P

"""

class SpriteManager:
    list = []
    
    previousTicks = 0
    
    def __init__(self):
        self.list = []
    
        self.previousTicks = 0
    
    #add a sprite to the manager
    def add(self, s):
        self.list.append(s)
    
    #Remove a sprite from the list
    def kill(self, i):
        self.list[i:1] = []
        
    #Update Animations
    def Update(self):
        ticks = pygame.time.get_ticks() - self.previousTicks
        self.previousTicks = pygame.time.get_ticks()
        for s in self.list:
            if isinstance(s, aSprite):
                s.ticks += ticks
                if(s.ticks >= s.fps):
                    s.Update()
                    s.ticks = 0
    
    #Draw all sprites on the screen
    def Draw(self, screen):
        for s in self.list:
            if (s.x + s.w > 0) and (s.y + s.h > 0):
                s.Draw(screen)